/*
 * @Author: 
 * @Date: 2022-02-09 14:42:44
 * @LastEditors: lqj
 * @LastEditTime: 2022-02-14 14:03:43
 * @Description: redux
 */
import React, { Component } from 'react'
import CountCenter from './components/CountCenter'
import store from '../../redux/store'
import { createIncrease, createIncreaseAsync, createClear } from '../../redux/module/count_redux'

class Count extends Component {
  componentDidMount() {
    store.subscribe(() => {
      this.setState({}) // 更新组件，调用render()
    })
  }
  handleChange = (val, count) => {
    switch (val) {
      case 'odd':
        if (store.getState().total % 2 !== 0) {
          store.dispatch(createIncrease(count))
        }
        break;
      case 'async':
        // setTimeout(() => {
        store.dispatch(createIncreaseAsync(count))
        // }, 700)
        break;
      case 'clear':
        store.dispatch(createClear(0))
        break;
      default:
        store.dispatch({ type: val, data: count })
        break;
    }
  }
  render() {
    return (
      <>
        <CountCenter count={store.getState().total} handleCount={this.handleChange} />
      </>
    );
  }
}

export default Count;

